package com.css.fxfzypg.modules.rating.expertRating.dao;

import com.css.fxfzypg.modules.rating.expertRating.model.entity.KpRatingTaskExpert;
import com.css.fxfzypg.modules.rating.task.model.entity.KpRatingTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class KpRatingTaskExpertDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    /**
     * 查询分配给某专家的考评任务
     *
     * @param expertId
     * @return
     */
    public List<KpRatingTask> getKpRatingTaskByExpertId(String expertId) {
        String sql = "select t2.* from kp_rating_task_expert t1 left join kp_rating_task t2 on t1.krt_id = t2.id where t2.del_flag = '0' and t1.expert_id = ? order by t2.create_time desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpRatingTask.class), expertId);
    }

    public void batchSave(List<KpRatingTaskExpert> expertList) {
        String sql = "INSERT INTO kp_rating_task_expert(id, expert_id, krt_id) values(:id, :expertId, :krtId)";
        namedParameterJdbcTemplate.batchUpdate(sql, SqlParameterSourceUtils.createBatch(expertList.toArray()));
    }

    public KpRatingTaskExpert getById(String krteId) {
        try {
            String sql = "SELECT * from kp_rating_task_expert WHERE id = ?";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(KpRatingTaskExpert.class), krteId);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
